package com.app.couponsharelife.ui.adapter

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.Poster
import com.app.couponsharelife.databinding.ShareInvitePicItemBinding
import com.app.couponsharelife.manage.Contacts.APP_LIFE_IMGURL
import com.app.couponsharelife.ui.listener.OnClickListener

class ShareInvitePicAdapter(private val inviteCode: String,
                            private val bitmap: Bitmap,
                            val list: List<Any>,
                            private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    private var isFirst = true

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.share_invite_pic_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (list.isNotEmpty()){
            val poster=list[position] as Poster
            Glide.with((holder as ViewHolder).itemView).asBitmap()
                    .apply(RequestOptions().transform(RoundedCorners( 2)))
                    .load(APP_LIFE_IMGURL+poster.path)
                    .into(holder.databinding.ivSharePic)
            holder.databinding.tvInviteCode.text = inviteCode
            holder.databinding.tvInviteCode.setTextColor(Color.parseColor("#${poster.fontColor}"))
            holder.databinding.tvStep1.setTextColor(Color.parseColor("#${poster.fontColor}"))
            holder.databinding.viewX.setBackgroundColor(Color.parseColor("#${poster.fontColor}"))
            holder.databinding.viewX1.setBackgroundColor(Color.parseColor("#${poster.fontColor}"))
            holder.databinding.tvStep2.setTextColor(Color.parseColor("#${poster.fontColor}"))
            /*if (position==1){
                holder.databinding.tvInviteCode.setTextColor(holder.itemView.resources.getColor(R.color.black))
                holder.databinding.tvStep1.setTextColor(holder.itemView.resources.getColor(R.color.black))
                holder.databinding.tvStep2.visibility= View.INVISIBLE
                holder.databinding.viewX.setBackgroundResource(R.color.black)
                holder.databinding.viewX1.setBackgroundResource(R.color.black)
            }else if(position==5){
                holder.databinding.tvInviteCode.setTextColor(holder.itemView.resources.getColor(R.color.red_b03))
                holder.databinding.tvStep1.setTextColor(holder.itemView.resources.getColor(R.color.red_b03))
                holder.databinding.tvStep2.setTextColor(holder.itemView.resources.getColor(R.color.red_b03))
                holder.databinding.tvStep2.visibility= View.VISIBLE
                holder.databinding.viewX.setBackgroundResource(R.color.red_b03)
                holder.databinding.viewX1.setBackgroundResource(R.color.red_b03)
            }else{
                holder.databinding.tvInviteCode.setTextColor(holder.itemView.resources.getColor(R.color.white))
                holder.databinding.tvStep1.setTextColor(holder.itemView.resources.getColor(R.color.white))
                holder.databinding.tvStep2.setTextColor(holder.itemView.resources.getColor(R.color.white))
                holder.databinding.tvStep2.visibility= View.VISIBLE
                holder.databinding.viewX.setBackgroundResource(R.color.white)
                holder.databinding.viewX1.setBackgroundResource(R.color.white)
            }*/
            Glide.with(holder.itemView).asBitmap()
                    .apply(RequestOptions().transform(RoundedCorners( 2)))
                    .load(bitmap)
                    .into(holder.databinding.ivInviteLinkQRCode)
            if (position == 1 && isFirst) {
                isFirst = false
                //holder.databinding.root.post { holder.databinding.root.performClick() }
            }
        }
    }

    inner class ViewHolder(val databinding: ShareInvitePicItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    val bitmap = Bitmap.createBitmap(databinding.root.width, databinding.root.height, Bitmap.Config.ARGB_8888)
                    val canvas = Canvas(bitmap)
                    databinding.root.draw(canvas)
                    listener.click(0, bitmap)
                }
            }
        }
    }
}